package com.credit.web.basic;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.crypto.Mac;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.context.CreditServletContext;
import com.credit.web.basic.bean.ResBean;
import com.qiniu.util.Auth;

@Controller
@RequestMapping("/image")
public class ImageController {
	private static final Log log = LogFactory.getLog(ImageController.class);
    private static String accessKey = "dyRRHwQFNCrm9eHDnZMZ_paThPCN0Wi_psXgzWTx";
    private static String secretKey = "TtVnM_wBDKgDYZGnIlXahFPEK4LOibg06p0OGtWL";
	
	@ResponseBody
	@RequestMapping(value = "/qiniuToken"/*, method = RequestMethod.POST*/)  
	public  void qiniuToken()  
	{  

	    String bucket = "xmmanager";
	    Auth auth = Auth.create(accessKey, secretKey);
	    String upToken = auth.uploadToken(bucket);
	    log.info("七牛返回的上传图片token [ "+upToken+" ]");
	    
		ResBean resBean = CreditServletContext.getResBean();
		resBean.getData().put("upToken", upToken);
		CreditServletContext.setResBean(resBean);
		
	    
	}  
	
	
	public String a() throws UnsupportedEncodingException{
	    String fileName = "七牛/云存储/qiniu.jpg";
	    String domainOfBucket = "http://devtools.qiniu.com";
	    String encodedFileName = URLEncoder.encode(fileName, "utf-8");
	    String finalUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
	    System.out.println(finalUrl);
	    return finalUrl;
	}
	
}
